import { ReactNode } from 'react';
import Image from 'next/image';
import Header from './headermanager';
import Link from './link';

export default function Panel({ title, children, menutype, btn }: { title: string; children: ReactNode; menutype: 1 | 2; btn?: { label: string; target: string; } }) {
	const menu = (() => {
		if (menutype === 2) {
			return [{
				label: '参赛项目',
				url: '/gov/home/1',
				active: true
			}];
		}
		return [{
			label: '参赛项目',
			url: '/teacher/home/1',
			active: true
		}];
	})();
	return <>
		<Header />
		<div className='cls004'>
			<div className='cls005'><Menu data={menu} /></div>
			<div className='cls001'>
				<div className='cls002'>
					<div className='cls003'>
						<Image width={25} height={25} src='/images/home.svg' />
						<span>{title}</span>
					</div>
					{btn && <div className='cls003'>
						<a target='_blank' rel='noreferrer' href={btn.target}>{btn.label}</a>
					</div>}
				</div>
				<div>{children}</div>
			</div>
		</div>
		<style jsx>{`
.cls005{
min-width: 15rem;
min-height: 45rem;
background-color: #f2f2f2;
}
.cls004{
display: flex;
justify-content: left;
}
a{
	background-color: #007fcb;;
	border-width: 1px;
	border-style: solid;
	border-color: #0462f7;
	border-radius: 4px;
	color: #fff;
	transition: background-color 600ms;
	padding-top: 0.4rem;
	padding-bottom: 0.4rem;
	padding-left: 1rem;
	padding-right: 1rem;
}
a:hover{
	background-color: #0a67fb;
}
.cls001{
margin-left: 2rem;
margin-right: 2rem;
margin-top:3rem;
}
.cls002{
display: flexbox;
justify-content: space-between;
margin: 2rem;
}
.cls003{
	padding: 1rem;
}
img{
	width: 25px;
}
span{
	font-family: '微软雅黑 Bold', '微软雅黑';
	font-weight: 700;
	font-style: normal;
	font-size: 18px;
	vertical-align: middle;
}
`}</style>
	</>;
}

interface IMenu {
	label: string;
	url?: string;
}

interface IMenu {
	label: string;
	active?: boolean;
	children?: IMenu[];
}

function Menu({ data }: { data: IMenu[]; }) {
	return <>
		<ol>
			{data.map((it, key) => {
				return <MenuItem key={key} data={it} />;
			})}
		</ol>
		<style jsx>{`
ol{
margin: 0;
padding: 2rem 0 10rem 0;
}
`}</style>
	</>;
}

function MenuItem({ data }: { data: IMenu; }) {
	const cls = data.active ? 'cls001' : 'cls002';
	return <>
		<li className={cls}>
			<span className='cls003'>
				<Image width={24} height={24} src='/images/bookmark.svg' />
			</span>
			{
				data.url ?
					<Link href={data.url}><span>{data.label}</span></Link>
					:
					<span>{data.label}</span>
			}
			{
				data.children && <ol>
					{data.children.map((it, key) => {
						return <MenuItem key={key} data={it} />;
					})}
				</ol>
			}
		</li>
		<style jsx>{`
li{
margin: 0;
padding: 1rem 1rem 1rem 1rem;
transition: 600ms;
}
.cls001{
	background-color: #69676781;
}
.cls002{
	background-color: #f2f2f2;
}
.cls001:hover,.cls002:hover{
	color: blue;
	background-color: #99999933;
}
li::marker{
content: '';
}
.cls003{
width: 1.5rem;
margin-right: 0.5rem;
}
`}</style>
	</>;
}
